<?php
/** Copyright 2014 TinPhp Systems, Inc.
 * @author zhangxilian 2013-8-15
 */
if (!defined('IN_TINPHP')) {
    exit('Access Denied');
}

require_once('indexApp.php');

class ArticleController extends IndexAppController {
	
	public function __construct() {
        parent::__construct();
        $this->artLogic = new ArticleLogic();
        $this->userLogic = new UserLogic();
        $this->commentLogic = new CommentLogic();
	}
 	
 	public function actionDefault(){	
        $page = intval($_REQUEST["p"]) ? $_REQUEST["p"] : 1;
        $catid = intval($_REQUEST["id"]) ? $_REQUEST["id"] : 1;
        $size = 5;
        $arrchildid = $this->get_arrchildid($catid); 
        $res = array();
        $par=array(
            "id" => $catid,
        );
        try{
            $res = $this->artLogic->getArtList("*", "CATID IN(".$arrchildid.") and state = 1", "order by id desc", $page, $size);
            $res['page'] = page :: listPage($par, $res["count"], $page, $size);
            
        }catch(Exception $e){
            echo $e->getMessage();
        }
        T::$tpl->set("res", $res);
 	}
    public function actionShow(){    

        $id = intval($_REQUEST['id']);
        $page = intval($_REQUEST["p"]) ? $_REQUEST["p"] : 1;
        $res = $this->artLogic->getArtItem("*","ID=".$id);
        $par=array(
             "id" => $id,
                );
        $comres = $this->commentLogic->getCommentList("*","STATE=1 and DATAID=".$id,false,$page , $size =10);
        $comres['page'] = page :: listPage($par,$comres['count'],$page,10);
        foreach($comres['data'] as $key => $value){
        if($value['USERID']){
          $userItem = $this->userLogic->getUserItem("*", "ID=".$value['USERID'], false); 
          $comres['data'][$key]['USERNAME']=$userItem['USERNAME'];
        }
        if($value['PARENTID']!=0){
        $puserid=$this->commentLogic->getCommentItem('USERID,PARENTID,ID','ID = '.$value['PARENTID'],false);
        $userid = $this->userLogic->getUserItem("*", "ID=".$puserid['USERID'], false);
        $comres['data'][$key]['PNAME']=$userid['USERNAME'];
        } 
        else {
        $comres['data'][$key]['PNAME']='';
        }
        }
        T::$tpl->set('res', $res);
        T::$tpl->set('comres', $comres);
//        T::$tpl->set('user', $this->user);
    }
    public function actionQueSave(){
        T::$tplFile = 'public/empty';
        $res = array();
        $res['code'] = 'error';
        $res['msg'] = '异常';
        $data=$_POST;
        $cond="USER_ID =".$data['user_id'];
        $res=$this->artLogic->getArtList2("ID,USER_ID", $cond, $order_by  = null, $page = null, $size = null);
        if($res["count"] > 5){
            $res['code'] = 'failure';
            $res['msg'] = '您提交的问题，我们将在段时间内解答，请耐心等待！';
            $res['tourl'] = 'index.php?ctl=article&id=15';
        }else{
        $data['state']=2;
        $art_Id=$this->artLogic->addArtItem($data);
        if ($art_Id){
            $res['code'] = 'success';
            $res['msg'] = '';
            $res['tourl'] = 'index.php?ctl=article&id=15';
        }
        }
       echo $this->getResources($res);
    }
    public function actionSave(){
        T::$tplFile = 'public/empty';
        $res = array();
        $res['code'] = 'error';
        $res['msg'] = '异常';
        $data=$_POST;
        $data['datatype']=1;
        $art_Id=$this->commentLogic->addComment($data);
        if ($art_Id){
            $res['code'] = 'success';
            $res['msg'] = '';
            $res['tourl'] = 'index.php?ctl=article&act=show&id='.$data['dataid'];
        }
       echo $this->getResources($res);
    }
}//End CLass
?>
